package com.kingtone.rongji.fragment;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.User;
import com.kingtone.rongji.network.UserRequest;
import com.kingtone.rongji.tool.JsonUtils;

public class PersonFragment extends BaseFragment {

	private TextView title;

	private ImageView backBtn;

	private TextView birthDay;
	
	private Spinner sexy;
	
	private EditText username;
	
	private EditText phone;
	
	private EditText email;
	
	private EditText name;

	private Calendar calendar = Calendar.getInstance();

	private Activity mActivity;
	
	private User user;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mActivity = activity;
	}

	public View onCreateView(LayoutInflater paramLayoutInflater,
			ViewGroup paramViewGroup, Bundle paramBundle) {
		View localView = paramLayoutInflater.inflate(R.layout.fragment_info,
				paramViewGroup, false);
		setupView(localView);
		initData();
		return localView;
	}

	private void initData() {
		// TODO Auto-generated method stub
		user = (User) this.getArguments().getParcelable("userInfo");
		if(user != null)
		{
			username.setText(user.getUserName());
			phone.setText(user.getCellPhone());
			email.setText(user.getEmail());
			name.setText(user.getRealName());
			sexy.setSelection(user.getGender());
			birthDay.setText(JsonUtils.convertToStringDateTime(user.getBirthDate()));
		}
	}

	private void setupView(View paramView) {
		title = (TextView) paramView.findViewById(R.id.title);
		title.setText(getString(R.string.profile_info));
		
		username = (EditText) paramView.findViewById(R.id.id_et_username);
		phone = (EditText) paramView.findViewById(R.id.id_et_phone);
		name = (EditText) paramView.findViewById(R.id.id_et_name);
		email = (EditText) paramView.findViewById(R.id.id_et_email);
		
		sexy = (Spinner) paramView.findViewById(R.id.id_sp_sexy);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this.getActivity(), R.array.sexy_item,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sexy.setAdapter(adapter);

		birthDay = (TextView) paramView.findViewById(R.id.id_tv_datePicker);
		birthDay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DatePickerDialog dpd = new DatePickerDialog(mActivity,
						dateListener, calendar.get(Calendar.YEAR), calendar
								.get(Calendar.MONTH), calendar
								.get(Calendar.DAY_OF_MONTH));
				dpd.show();
			}
		});

		backBtn = (ImageView) paramView.findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});
		
		Button save = (Button) paramView.findViewById(R.id.id_bt_next);
		save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				UserRequest userRequest = new UserRequest();
				userRequest.update(user, user.getUserName(), email
						.getText().toString(), JsonUtils.convertToJsonDateTime(birthDay.getText().toString()),
						name.getText().toString(), phone.getText().toString(), sexy.getSelectedItemPosition(), PersonFragment.this);
			}
		});
	}

	DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, monthOfYear);
			calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			String str = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
			birthDay.setText(str);
		}
	};
	
	@Override
	public void onBussinessSuccess(Object obj) {
		super.onBussinessSuccess(obj);
		getActivity().onBackPressed();
	}
}
